var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        client:'',
        platform:'all',
        page: 1,
        listRows: 10,
        total: 0,
        realName:'',
        isVip:'',
        mobile:'',
        idArrUnique:[],
        nochecked:[]
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function() {
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            mobile: _this.params.mobile,
            order: _this.params.order,
            endTime:_this.params.endTime,
            startTime:_this.params.startTime,
            isVip: _this.params.isVip,
            realName:_this.params.realName,

        };
        console.log(paramsObj);
        Api.userinfolist(paramsObj, function(data) {
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            mobile: _this.params.mobile,
                            order: _this.params.order,
                            endTime:_this.params.endTime,
                            startTime:_this.params.startTime,
                            isVip: _this.params.isVip,
                            realName:_this.params.realName,
                        };
                        if(!first){
                          Api.userinfolist(Params, function(data) {
                                _this.getDataList(data.list);
                                //console.log(_this.params.idArrUnique);
                                $('input[type=checkbox][name=new]').each(function() {
                                    var self=$(this);
                                    _this.params.idArrUnique.forEach(function(item,index){
                                        if (self.attr('userId')==item) {
                                            self.iCheck('check');
                                        }
                                    });
                                });
                                if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                                     $('input[type=checkbox][name=all]').parent().addClass('checked');
                                }else{
                                    $('input[type=checkbox][name=all]').parent().removeClass('checked');
                                };
                            })
                        }

                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                    '<td class="text-c">'+
                        '<div class="goods-checkbox">'+
                            '<div class="check-box" userId="' + data[i].userId + '">'+
                                '<input type="checkbox"  name="new" userId="' + data[i].userId + '">'+
                            '</div>'+
                        '</div>'+
                    '</td>'+
                    '<td class="text-c">' + data[i].userId + '</td>' +
                    '<td class="text-c">' + data[i].mobile + '</td>';
                if (data[i].isVip == 1) {
                    html += '<td class="text-c">会员</td>';
                } else {
                    html += '<td class="text-c">非会员</td>';
                };
                    html += '<td class="text-c">' + data[i].realName + '</td>' +

                    '</tr>';
            }

            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="5">暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('input[type=checkbox][name=all]').on('ifChanged', function() {
            if ($(this).parent().hasClass('checked')) {
                $('input[type=checkbox][name=new]').iCheck('uncheck');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('unchecked');
                },100)
            } else {
                $('input[type=checkbox][name=new]').iCheck('check');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('checked');
                },100)
            }
        });
        setTimeout(function(){
             $('input[type=checkbox][name=new]').on('ifChanged', function() {
                //获取选取的并加入数组
                _this.params.idArrUnique.push($(this).attr('userId'));
                //数组去重
                _this.params.idArrUnique=_this.params.idArrUnique.unique();
                // console.log(_this.params.idArrUnique);
                //获取未选取的并加入数组
                for (var i = 0; i < $('input[type=checkbox][name=new]').length; i++) {
                     if ($('input[type=checkbox][name=new]').eq(i).prop('checked')) {
                        _this.params.nochecked.remove($('input[type=checkbox][name=new]').eq(i).attr('userId'));
                        _this.params.nochecked=_this.params.nochecked.unique();
                     }else{
                        _this.params.nochecked.push($('input[type=checkbox][name=new]').eq(i).attr('userId'));
                        _this.params.nochecked=_this.params.nochecked.unique();
                     };
                };
                //去除x选取数组中未选取的ID
                for (var i = 0; i < _this.params.nochecked.length; i++) {
                    // console.log(nochecked[i]);
                    _this.params.idArrUnique.remove(_this.params.nochecked[i]);
                };
                if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                     $('input[type=checkbox][name=all]').parent().addClass('checked');
                }else{
                    $('input[type=checkbox][name=all]').parent().removeClass('checked');
                };
                console.log(_this.params.idArrUnique);
            });
         },500)


    },
    getallclient:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.getallclient(paramsObj, function(data){
            console.log(data);
            _this.getallclientList(data);
        });
    },
    getallclientList:function(data){
        var _this = this;
        if (data) {
            var html = '';
            for(i in data){
              html +='<span class="btn btn-default-outline key" style="width:120px;" keys="'+i+'">'+data[i]+'</span>';
            }
            $('#obj').html(html);
            _this.getallclientEvent();
        } else {
            var html = '<span>暂无数据</span>'
            $('#obj').html(html);
        }
    },
    getallclientEvent:function(){
        var _this = this;
        $('.key').eq(0).addClass('act');
        _this.params.client=$('.key').eq(0).attr('keys');
        $('.key').click(function(event) {
           $('.key').removeClass('act');
           $(this).addClass('act');
           _this.params.client=$(this).attr('keys');

        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('.plat').click(function(event) {
           $('.plat').removeClass('act');
           $(this).addClass('act');
           _this.params.platform=$(this).attr('data');
        });
        $('#vipSelect').on('change', function(event) {
            event.preventDefault();

            _this.params.isVip = $(this).val();
        });
        $('#normalSearchs').click(function(event) {

            _this.params.mobile = $('#mobile').val();
            _this.params.realName = $('#realName').val();
            _this.params.page = 1;
            _this.getData();
        });
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#time',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var startTime = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var endTime = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 23:59:59';
                        _this.params.startTime = startTime;
                        _this.params.endTime = endTime;

                    } else {
                        _this.params.startTime = '';
                        _this.params.endTime = '';
                    };
                }
            });
        });
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {
            var mapId='';
            if (_this.params.idArrUnique.length>0) {
                mapId=_this.params.idArrUnique.join(",");
            }
            var paramsObj = {
                passport: _this.params.passport,
                mapId:mapId,
                client:_this.params.client,
                title:Pub.getSession('title'),
                desc:Pub.getSession('desc'),
                releaseStatus:Pub.getSession('releaseStatus'),
                subscribeTime:Pub.getSession('subscribeTime'),
                isPush:Pub.getSession('isPush'),
                pushType:Pub.getSession('pushType'),
                content:Pub.getSession('content'),
                url:Pub.getSession('url'),
                remark:Pub.getSession('remark'),
                platform:_this.params.platform
            };
            console.log(paramsObj);
            if (mapId=='') {
                layui.use('layer', function(){
                    layer.msg('请先选择推送用户', {icon: 5});
                });
            }else{
                Api.releaseactivitymessage(paramsObj, function(data){
                    console.log(data);
                    layui.use('layer', function(){
                        layer.msg('发布成功', {icon: 6});
                        setTimeout(function(){
                            window.location.href = Pub.getHtmlUrl('./activity-list.html');
                        },1500)
                    });
                });
            };


        });
    },
    getAllId:function(){
        var _this = this;
        $('#selectAll').click(function(event) {
            var paramsObj = {
                passport: _this.params.passport,
                page: 1,
                listRows: 10000000,
            };
            console.log(paramsObj);
            var isOrNo=$(this).attr('data');
            var self=$(this);
            Api.userinfolist(paramsObj, function(data){
                console.log(data);
                if (data.list.length>0) {
                    if (isOrNo==0) {
                        $('input[type=checkbox][name=all]').iCheck('check');
                        setTimeout(function(){
                            _this.params.idArrUnique=[];
                            for (var i = 0; i < data.list.length; i++) {
                                _this.params.idArrUnique.push(data.list[i].userId);
                            };
                            console.log(_this.params.idArrUnique);
                            self.attr('data', '1');
                        },200)
                    }else{
                        $('input[type=checkbox][name=all]').iCheck('uncheck');
                        setTimeout(function(){
                            _this.params.idArrUnique=[];
                            self.attr('data', '0');
                            console.log(_this.params.idArrUnique);
                        },200)
                    };

                };
            });
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.getallclient();
        _this.otherEvent();
        _this.submit();
        _this.getAllId();

    }
};
var page = new Page();